#include "fileControl.hpp"
#include <regex>

fileControl::fileControl(std::filesystem::path workDir_root)
  : root_path(workDir_root)
{
}

bool fileControl::CreateProjectDir(const std::string & project_dir_name)
{
  return std::filesystem::create_directory(root_path/project_dir_name);
}

bool fileControl::CreateTaskDir(const std::string & project_dir_name,const std::string & task_dir_name)
{
  return std::filesystem::create_directory(root_path/project_dir_name/task_dir_name);
}

std::vector<std::string> fileControl::ScanTaskDir(const std::string & project_dir_name)
{
  std::vector<std::string> list;
  for(auto const & dir_entry : std::filesystem::directory_iterator(root_path/project_dir_name))
    list.push_back(dir_entry.path().filename());
  return std::move(list);
}

std::vector<std::string> fileControl::ScanProjectDir()
{
  std::vector<std::string> list;
  for(auto const & dir_entry : std::filesystem::directory_iterator(root_path))
    list.push_back(dir_entry.path().filename());
  return std::move(list);
}

std::vector<std::string> fileControl::ScanVideoFile(const std::string & project_dir,const std::string & task_dir)
{
  std::vector<std::string> list;
  std::regex regMKV(R"([\w]+\.mkv)");
  std::smatch regRes;
  for(auto const & dir_entry : std::filesystem::directory_iterator(root_path/project_dir/task_dir))
  {
    std::string fileName=dir_entry.path().filename();
    if(std::regex_match(fileName,regRes,regMKV))
      list.push_back(dir_entry.path().filename());
  }
  return std::move(list);
}

bool fileControl::CheckVideoFile(const std::string & project_dir,const std::string & task_dir,const std::string& videoFile)
{
  return std::filesystem::exists(root_path/project_dir/task_dir/videoFile);
}
